package com.fh.fruit.controller.setMeal;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.goods.GoodsUnit;
import com.fh.fruit.service.goods.GoodsManager;
import com.fh.fruit.service.period.PeriodManager;
import com.fh.fruit.service.setMeal.SetMealManager;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 套餐Controller类
 */
@Controller
@RequestMapping(value = "setMeal")
public class SetMealController extends BaseController {
	String menuUrl = "setMeal/list.do";
	
	@Resource(name = "setMealService")
    private SetMealManager setMealManager;
	
	@Resource(name = "periodService")
	private PeriodManager periodService;
	
	@Resource(name = "goodsService")
	private GoodsManager goodsService;

	/**显示套餐列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView listGoods(Page page,HttpSession httpSession) throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		PageData pd1 = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		page.setPd(pd);
		List<Map<String,Object>> mealList = setMealManager.getMealList(page);
		pd1.put("updateId", user.getUSER_ID()); // 修改人
		pd1.put("updateTime", DateUtil.getTime()); // 修改时间
		for (Map<String, Object> map : mealList) {
			Integer stock_num=(Integer)map.get("stock_num");
			pd1.put("id", map.get("id"));
			pd1.put("state", "00");
			if(stock_num == 0){
				setMealManager.updateSaleState(pd1);
			}
		}
		mv.setViewName("farm/setMeal/meal_list");
		mv.addObject("pd", pd);
		mv.addObject("mealList", mealList);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**
	 * 去新增套餐页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddMeal")
	public ModelAndView goAddMeal() throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		mv.setViewName("farm/setMeal/meal_edit");
		mv.addObject("pd", pd);
		mv.addObject("msg", "saveMeal");
		return mv;
	}
	
	/**
	 * 新增商品套餐
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveMeal")
	public ModelAndView saveMeal(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增商品套餐");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("createTime", DateUtil.getTime()); // 创建时间
		int num = setMealManager.saveMeal(pd);
		if(num ==0){
			mv.addObject("msg", "套餐保存失败!");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去修改套餐页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEditMeal")
	public ModelAndView goEditMeal() throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		Map<String,Object> meal =setMealManager.getMealMsg(pd);
		mv.setViewName("farm/setMeal/meal_edit");
		mv.addObject("pd", pd);
		mv.addObject("meal", meal);
		mv.addObject("msg", "updateMeal");
		return mv;
	}
	
	/**
	 * 保存商品
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateMeal")
	public ModelAndView updateMeal(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "修改套餐");
		ModelAndView mv = this.getModelAndView();
		PageData pd = null;
		try {
			pd = this.getPageData();
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("updateId", user.getUSER_ID()); // 修改人
			pd.put("updateTime", DateUtil.getTime()); // 修改时间
			int num=setMealManager.updateMeal(pd);
			if(num==0){
				mv.addObject("msg", "套餐修改失败!");
			} /*else if(num==2){
				mv.addObject("msg", "套餐已下单,信息不能修改!");
			}*/
		} catch (Exception e) {
			logger.error(e.toString(), e);
			mv.addObject("msg", "failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去套餐商品列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goMealGoods")
	public ModelAndView goMealGoods(Page page) throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		page.setShowCount(6);
		List<Map<String,Object>> mealGoods =setMealManager.getMealGoodsList(page);
		mv.setViewName("farm/setMeal/meal_goods_list");
		mv.addObject("pd", pd);
		mv.addObject("mealGoods", mealGoods);
		mv.addObject("QX",Jurisdiction.getHC());
		return mv;
	}
	
	/**
	 * 去新增套餐商品页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddMealGoods")
	public ModelAndView goAddMealGoods() throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		List<Map<String, Object>> baseList = periodService.getBaseList();
		List<GoodsUnit> unit = goodsService.getGoodsUnitList();
		mv.setViewName("farm/setMeal/meal_goods_edit");
		mv.addObject("pd", pd);
		mv.addObject("baseList", baseList);
		mv.addObject("unit", unit);
		mv.addObject("msg", "saveMealGoods");
		return mv;
	}
	
	/**
	 * 去修改套餐商品页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEditMealGoods")
	public ModelAndView goEditMealGoods() throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		Map<String,Object> meal =setMealManager.getMealGoods(pd);
		List<Map<String, Object>> baseList = periodService.getBaseList();
		List<GoodsUnit> unit = goodsService.getGoodsUnitList();
		mv.setViewName("farm/setMeal/meal_goods_edit");
		mv.addObject("pd", pd);
		mv.addObject("meal", meal);
		mv.addObject("baseList", baseList);
		mv.addObject("unit", unit);
		mv.addObject("msg", "updateMealGoods");
		return mv;
	}
	
	/**
	 * 选择商品页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectGoodsList")
	public ModelAndView selectGoodsList(Page page) throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		page.setShowCount(6);
		List<Map<String,Object>> goodsList=setMealManager.getGoodsSpecList(page);
		mv.setViewName("farm/setMeal/selectGoodsList");
		mv.addObject("pd", pd);
		mv.addObject("goodsList", goodsList);
		mv.addObject("QX",Jurisdiction.getHC());
		return mv;
	}
	
	/**
	 * 新增商品套餐
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveMealGoods")
	public ModelAndView saveMealGoods(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增商品套餐");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("createTime", DateUtil.getTime()); // 创建时间
		int num = setMealManager.saveMealGoods(pd);
		if(num ==0){
			mv.addObject("msg", "套餐保存失败!");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 删除套餐商品
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteMealGoods")
	@ResponseBody
	public String deleteMealGoods(HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除商品");
		PageData pd = this.getPageData();
		try {
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("updateId", user.getUSER_ID()); // 创建人
			pd.put("updateTime", DateUtil.getTime()); // 创建时间
			int num=setMealManager.deleteMealGoods(pd);
			if(num ==0){
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
		return "success";
	}
	
	/**
	 * 修改上下架状态
	 */
	@RequestMapping(value = "/updateMealState")
	@ResponseBody
	public String updateMealState(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "update")) {
			return null;
		} // 校验权限
		PageData pd = this.getPageData();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		String endTime= pd.getString("endTime");
		String state= pd.getString("state");
		Date et= sdf.parse(endTime);
		Date nt=sdf.parse(sdf.format(date));
		if("01".equals(state)){
			 if (et.getTime() <= nt.getTime()) {
				 return "timeEnd";
			 }
			 page.setPd(pd);
			 List<Map<String,Object>> mealGoods =setMealManager.getMealGoodsList(page);
			 if(mealGoods ==null || mealGoods.size()==0){
				 return "no-goods";
			 }
		}
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("updateId", user.getUSER_ID()); // 修改人
		pd.put("updateTime", DateUtil.getTime()); // 修改时间
		int num=setMealManager.updateMealState(pd);
		return num==1?"success":"failed";
	}
	
	/**
	 * 修改套餐商品
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateMealGoods")
	public ModelAndView updateMealGoods(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "update")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "修改套餐商品");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("updateTime", DateUtil.getTime()); // 创建时间
		int num = setMealManager.updateMealGoods(pd);
		if(num ==0){
			mv.addObject("msg", "套餐修改失败!");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	@RequestMapping(value = "updateSaleState")
	@ResponseBody
	public String updateSaleState(Page page, HttpSession httpSession) throws Exception {
		PageData pd = this.getPageData();
		try {
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("updateId", user.getUSER_ID()); // 修改人
			pd.put("updateTime", DateUtil.getTime()); // 修改时间
			int num = setMealManager.updateSaleState(pd);
			if (num == 0) {
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}

		return "success";
	}
}
